package ext.tianma.listener;

import ext.com.iba.IBAUtil;
import ext.tianma.bom.util.GenericUtil;
import ext.tianma.doc.forms.CreateNewDocProcessor;
import ext.tianma.util.FileDownloadUtil;
import org.apache.log4j.Logger;
import wt.content.*;
import wt.doc.WTDocument;
import wt.doc.WTDocumentDependencyLink;
import wt.fc.*;
import wt.folder.Folder;
import wt.folder.FolderEntry;
import wt.folder.FolderHelper;
import wt.inf.container.WTContainer;
import wt.inf.container.WTContainerRef;
import wt.log4j.LogR;
import wt.type.TypeDefinitionReference;
import wt.type.TypedUtility;
import wt.util.WTException;
import wt.util.WTProperties;
import wt.vc.VersionControlHelper;
import wt.vc.wip.WorkInProgressHelper;

import java.beans.PropertyVetoException;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import java.util.List;

/**
 * @author MaggieHao
 * @version 1.0
 * @description 工艺文件功能优化
 * 1、新建工艺类原档文档类型，上传工艺文件附件自动创建工艺类原档文档类型，
 * 2、建立关联关系，
 * 3、在工艺文件上增加成品型号属性用于将工艺文件关联到成品上
 * @date 2019/7/8 11:08
 **/

public class ProcessOriginListenerService {

    private static final Logger LOGGER = LogR.getLogger(ProcessOriginListenerService.class.getName());
    private static final String PRIMARY_FILE_TEMP = "PrimaryFileTemp";
    private static final String PROCESS_DOCUMENTS = "ProcessDocuments";

    private static String folderPath;

    static {
        try {
            String templateFolder = WTProperties.getLocalProperties().getProperty("wt.home");
            folderPath = templateFolder + File.separator + "codebase" + File.separator + "temp";
        } catch (IOException e) {
            LOGGER.error("", e);
        }
    }

    /**
     * 创建工艺文件时，创建工艺类原档图纸,并创建说明关系
     *
     * @param targetObj
     * @param eventType
     **/
    public static void createProcessOriginDocAndLink(Object targetObj, String eventType, String processDocument, String originDrawingDoc) throws Exception {
        System.out.println("===创建工艺文件 beginning===");
        String softType;
        if (eventType.equals(ContentServiceEvent.POST_UPLOAD)) {
            System.out.println("===POST_UPLOAD-创建工艺文件=========");
            try {
                System.out.println(" :" + targetObj + "===flag:" + (targetObj instanceof WTDocument));
                if (targetObj instanceof WTDocument) {
                    WTDocument doc = (WTDocument) targetObj;
                    String docFolderPath1 = doc.getFolderPath();
                    System.out.println("创建工艺类原档图纸-doc 文件路径-最开始获取路径地址：" + docFolderPath1);
                    //在创建工艺文件的时候就将文件的路径保存到 docFilePath中
                    //工艺文档,设置工艺文档属性文件存储位置
                    String docFolderPath = ((String) IBAUtil.getIBAValue(doc, "docFilePath"));
                    if (docFolderPath == null) {
                        Hashtable<String, String> ibaMap = new Hashtable<String, String>();
                        addToHashtable(ibaMap, "docFilePath", docFolderPath1);
                        IBAUtil.setSoftTypeIBAValues(doc, ibaMap);
                        PersistenceHelper.manager.refresh(doc);
                    }

                    System.out.println("通过属性获取的位置信息：" + docFolderPath);
                    softType = GenericUtil.getTypeId(doc);
                    System.out.println("softType:" + softType);
                    // 判断类型为工艺类文件 且 新建时
                    // 获取工艺类文件附件
                    ApplicationData appData = null;

                    QueryResult primaryResult = ContentHelper.service.getContentsByRole(doc, ContentRoleType.PRIMARY);
                    QueryResult secondResult = ContentHelper.service.getContentsByRole(doc, ContentRoleType.SECONDARY);
                    System.out.println("===primaryResult:" + primaryResult.size());
                    System.out.println("===secondResult:" + secondResult.size());
                    String docName = "";
                    while (secondResult.hasMoreElements()) {
                        ContentItem attachment = (ContentItem) secondResult.nextElement();
                        System.out.println("创建工艺文件-attachment：" + attachment.getFormatName());
                        if (attachment instanceof ApplicationData) {
                            appData = (ApplicationData) attachment;
                            if (appData != null) {
                                System.out.println("===appData:" + appData.getFileName());
                                System.out.println("===appData Path:" + appData.getUploadedFromPath());
                                docName = appData.getFileName();

                                if (docName != "" || docName != null) {
                                    docName = subFileName(docName);
                                }
                                System.out.println("docNameByApp======>>>>" + docName);
                            }
                        }
                        break;
                    }
                    ObjectReference derived = VersionControlHelper.getDerivedFrom(doc);
                    if (derived == null || derived.getObject() == null && appData != null && softType.contains(processDocument)) {
                        // 新建
                        System.out.println("NEW_VERSION.............create:" + doc.getNumber());
                        String version = VersionControlHelper.getIterationDisplayIdentifier(doc).toString();
                        if ("0.0".equals(version)) {
                            boolean checkout = WorkInProgressHelper.isCheckedOut(doc);
                            if (!checkout) {
                                System.out.println("创建工艺文件-doc位置：" + doc.getFolderPath());
                                // 创建工艺类原档图纸
                                WTDocument document = createOriginDrawingDoc(doc, docName, originDrawingDoc);
                                if (document != null) {
                                    System.out.println("=====document:" + document.getNumber());
                                }
                                // 工艺类原档图纸挂载主内容
                                document = addContent(document, appData);
                                // 创建说明关系
                                createDescripLink(document, doc);
                            }
                        }

                    } else {
                        System.out.println("NEW_VERSION.............new:" + doc.getNumber());
                    }

                }


            } catch (WTException e) {
                LOGGER.error("", e);
                e.printStackTrace();
            }
        }
        System.out.println("===创建工艺文件 ending===maggie");

    }

    /**
     * 实现Hashtable添加数据时不会出现空指针异常
     *
     * @param hashtable
     * @param key
     * @param value
     * @return
     */
    private static Hashtable<String, String> addToHashtable(Hashtable<String, String> hashtable, String key,
                                                            String value) {
        if (key == null && hashtable != null) {
            return hashtable;
        }

        if (value == null && hashtable != null) {
            return hashtable;
        }

        if (key == null && hashtable == null) {
            return new Hashtable<String, String>();
        }

        if (value == null && hashtable == null) {
            return new Hashtable<String, String>();
        }

        if (hashtable == null) {
            hashtable = new Hashtable<String, String>();
        }
        hashtable.put(key, value);

        return hashtable;
    }

    /**
     * 创建工艺类原档图纸
     *
     * @param doc
     * @return
     */
    public static WTDocument createOriginDrawingDoc(WTDocument doc, String docName, String originDrawingDoc, String docFolderPath) {
        System.out.println("创建工艺类原档图纸-docName：" + docName);
        WTContainer container = doc.getContainer();

        System.out.println("创建工艺类原档图纸-doc 文件路径：" + docFolderPath);
        System.out.println("容器：" + container.getName());
        if (container == null) {
            System.out.println("the WTDocument getContainer is null!");
            return null;
        }
        WTDocument document = null;
        try {
            document = WTDocument.newWTDocument();
            System.out.println("docName:" + docName);
            // 名称
            document.setName(docName);

            // 类型
            TypeDefinitionReference typeDefinitionRef = TypedUtility.getTypeDefinitionReference(originDrawingDoc);
            document.setTypeDefinitionReference(typeDefinitionRef);
            // 存储容器
            WTContainerRef ref = WTContainerRef.newWTContainerRef(container);
            document.setContainerReference(ref);
            System.out.println("创建工艺类原档图纸-document:" + document.getName());

            // 具体位置 与工艺类文档相同
            Folder oldFoler = FolderHelper.getFolder(doc);
//            Folder oldFoler = FolderUtil.getFolder("/Default", doc.getContainerReference());

            if (oldFoler == null) {
                String strLocation = doc.getLocation();
                oldFoler = FolderHelper.service.getFolder(strLocation, container.getContainerReference());
                System.out.println("工艺文件夹：" + oldFoler);
            }
            Folder newFolder = FolderHelper.service.saveFolderPath(docFolderPath, ref);
            System.out.println("getContainerDisplayName:" + newFolder.getContainerDisplayName() + ",getFolderPath" + newFolder.getFolderPath());
            if (newFolder == null) {
                String strLocation = doc.getLocation();
                newFolder = FolderHelper.service.getFolder(strLocation, container.getContainerReference());
                System.out.println("工艺文件夹：" + newFolder);
            }
            FolderHelper.assignLocation((FolderEntry) document, newFolder);

            // 持久化
            document = (WTDocument) PersistenceHelper.manager.save(document);

        } catch (Exception e) {
            LOGGER.error("", e);
            e.printStackTrace();
        }
        return document;

    }

    /**
     * 创建工艺类原档图纸
     *
     * @param doc
     * @return
     */
    public static WTDocument createOriginDrawingDoc(WTDocument doc, String docName, String originDrawingDoc) {
        System.out.println("创建工艺类原档图纸-docName：" + docName);
        WTContainer container = doc.getContainer();

        System.out.println("创建工艺类原档图纸-doc 文件路径：" + doc.getFolderPath());

        if (container == null) {
            System.out.println("the WTDocument getContainer is null!");
            return null;
        }
        WTDocument document = null;
        try {
            document = WTDocument.newWTDocument();
            System.out.println("docName:" + docName);
            // 名称
            document.setName(docName);

            // 类型
            TypeDefinitionReference typeDefinitionRef = TypedUtility.getTypeDefinitionReference(originDrawingDoc);
            document.setTypeDefinitionReference(typeDefinitionRef);
            // 存储容器
            WTContainerRef ref = WTContainerRef.newWTContainerRef(container);
            document.setContainerReference(ref);
            System.out.println("创建工艺类原档图纸-document:" + document.getName());

            // 具体位置 与工艺类文档相同
            Folder oldFolder = FolderHelper.getFolder(doc);
            System.out.println("创建工艺类原档图纸-getFolderPath：" + oldFolder.getFolderPath());

            if (oldFolder == null) {
                String strLocation = doc.getLocation();
                oldFolder = FolderHelper.service.getFolder(strLocation, container.getContainerReference());
                System.out.println("工艺文件夹：" + oldFolder);
            }
            FolderHelper.assignLocation((FolderEntry) document, oldFolder);
            // 持久化
            document = (WTDocument) PersistenceHelper.manager.save(document);

        } catch (Exception e) {
            LOGGER.error("", e);
            e.printStackTrace();
        }
        return document;

    }

    /**
     * 工艺类原档图纸挂载主内容
     *
     * @param doc
     * @param data
     * @return
     */
    public static WTDocument addContent(WTDocument doc, ApplicationData data) {
        if (doc != null && data != null) {
            System.out.println("新增主内容方法-doc:" + doc.getName() + "data:" + data.getFileName());
            try {
                ApplicationData newData = ApplicationData.newApplicationData(doc);
                newData.setRole(ContentRoleType.PRIMARY);

                System.out.println("将原档图纸挂载主内容-doc:" + doc.getName());
                System.out.println("===========addContent data:" + data.getFileName());
                System.out.println("===========addContent info:" + data.getPersistInfo());

                String path = FileDownloadUtil.FolderPath + File.separator + PRIMARY_FILE_TEMP + File.separator
                        + PROCESS_DOCUMENTS + File.separator + data.getFileName();

                System.out.println("下载主内容的path:" + path);
                boolean downloadPrimaryFileFlag = downloadPrimaryFile(doc, PROCESS_DOCUMENTS, data.getFileName(), data);//下载主内容到指定文件夹
                System.out.println("=========downloadPrimaryFileFlag:" + downloadPrimaryFileFlag);

                if (downloadPrimaryFileFlag) {
                    newData.setFileName(data.getFileName());
                    newData.setUploadedFromPath(path);
                    newData = ContentServerHelper.service.updateContent(doc, newData, path);
                }

                System.out.println("maggie-newData:" + newData.getFileName());
                PersistenceHelper.manager.save(newData);

                doc = (WTDocument) PersistenceHelper.manager.refresh(doc);
                PersistenceServerHelper.manager.update(doc);
                File file = new File(path);
                if (file.exists()) {
                    file.delete();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return doc;
    }

    /**
     * 移除工艺文档附件
     *
     * @param doc
     * @return
     */
    public static WTDocument deleteContent(WTDocument doc) {
        if (doc != null) {
            QueryResult result;
            try {
                result = ContentHelper.service.getContentsByRole(doc, ContentRoleType.SECONDARY);

                ApplicationData data = null;
                while (result.hasMoreElements()) {
                    data = (ApplicationData) result.nextElement();
                }
                System.out.println("删除附件=========deleteContent:" + data);
                if (data != null) {
                    ContentServerHelper.service.deleteContent(doc, data);
                    System.out.println("maggie=========deleteContent end:" + data);
                }
            } catch (Exception e) {
                LOGGER.error("", e);
                e.printStackTrace();
            }
        }
        return doc;
    }

    /**
     * 工艺文档关联原档图纸
     *
     * @param processDoc
     * @param document
     */
    public static void createDescripLink(WTDocument processDoc, WTDocument document) {
        WTDocumentDependencyLink dependencyLink;
        try {
            if (processDoc != null && document != null) {
                System.out.println("创建说明关系-processDocName:" + processDoc.getName() + "documentName:" + document.getName());
                dependencyLink = WTDocumentDependencyLink.newWTDocumentDependencyLink(document, processDoc);
                PersistenceServerHelper.manager.insert(dependencyLink);
            }
        } catch (WTException e) {
            LOGGER.error("", e);
            e.printStackTrace();
        }

    }

    /**
     * 创建文件夹
     *
     * @param diskPath
     * @return
     */

    public static String createDiskFolder(String diskPath) {
        File file = new File(diskPath);
        if (!file.exists()) {// 如果文件夹不存在
            file.mkdirs();// 创建文件夹
        }
        return diskPath;
    }

    /**
     * 下载主文件
     *
     * @param contentHolder
     * @param folderName
     * @return
     * @throws WTException
     */
    public static boolean downloadPrimaryFile(ContentHolder contentHolder, String folderName, String fileName,
                                              ApplicationData data) throws WTException {

        String tempFolderPath = folderPath + File.separator + PRIMARY_FILE_TEMP + File.separator + folderName;// 文件夹
        System.out.println("下载文件文件夹路径：" + tempFolderPath);
        createDiskFolder(tempFolderPath);
        FormatContentHolder formatContentHolder = null;
        boolean rtnValue = true;
        if (contentHolder == null) {
            return false;
        }
        try {
            formatContentHolder = (FormatContentHolder) ContentHelper.service.getContents(contentHolder);
            System.out.println("formatContentHolder.getFormatName():" + formatContentHolder.getFormatName());
        } catch (PropertyVetoException e1) {
            e1.printStackTrace();
        }

        ApplicationData primary = (ApplicationData) ContentHelper.getPrimary(formatContentHolder);
        if (data != null) {
            primary = data;
        }
        System.out.println("=========downloadPrimaryFile primary:" + primary);
        if (primary != null) {
            try {
                if (!"".equals(fileName) && fileName != null) {
                    tempFolderPath = tempFolderPath + File.separator + fileName;

                }
                ContentServerHelper.service.writeContentStream(primary, tempFolderPath);
            } catch (IOException e) {
                System.out.println("写文件错误" + tempFolderPath);
                rtnValue = false;
            }
        } else {
            rtnValue = false;
        }

        return rtnValue;
    }

    /**
     * 编辑或 修订 工艺文件时  更新附件内容至参考文档（工艺类原档图纸的主内容）
     * 修改文件名称
     *
     * @param targetObj
     * @param eventType
     * @throws Exception
     */
    public static void modifyProcessOriginDocAndLink(Object targetObj, String eventType, String processDocument, String originDrawingDoc) throws Exception {
        System.out.println("===修改工艺文件 beginning===maggie");

        if (eventType.equals(PersistenceManagerEvent.POST_MODIFY)) {
            WTDocument doc = null;
            if (targetObj instanceof WTDocument) {

                doc = (WTDocument) targetObj;

                String docGetPath = doc.getFolderPath();
                System.out.println("工艺文件 doc.getFolderPath" + docGetPath);
                String docFolderPath = "";

                System.out.println("getFolderingInfo:" + doc.getFolderingInfo().getLocation() + "getParentFolder:" + doc.getParentFolder().getName());
                if (IBAUtil.getIBAValue(doc, "docFilePath") != null) {
                    docFolderPath = ((String) IBAUtil.getIBAValue(doc, "docFilePath"));
                    System.out.println("通过属性获取的位置信息：" + docFolderPath);
                }

                System.out.println("工艺文档名称：" + doc.getName());

                String softType = GenericUtil.getTypeId(doc);

                // 判断类型为工艺类文件
                System.out.println("====softType:" + softType);
                if (!softType.contains(processDocument)) {
                    return;
                }


                QueryResult primary = ContentHelper.service.getContentsByRole(doc, ContentRoleType.PRIMARY);

                ContentItem attachmentPrimary = (ContentItem) primary.nextElement();

                System.out.println("检入修改文件名称~");
                //对文档名称更改
                if (attachmentPrimary instanceof ApplicationData) {
                    ApplicationData appDataPrimary = (ApplicationData) attachmentPrimary;
                    String appPrimaryName = appDataPrimary.getFileName();
                    appPrimaryName = subFileName(appPrimaryName);
                    System.out.println("appDataPrimary Name:" + appPrimaryName);
                    GenericUtil.setWTDocName(doc, appPrimaryName);
                }

                System.out.println(attachmentPrimary);
                System.out.println("===ProcessDocument primary:" + primary.size());


                //判断 是否有附件
                boolean isProcessDocument = CreateNewDocProcessor.checkAttacheByDOC(doc);
                if (!isProcessDocument) {
                    System.out.println("工艺文档没有附件");
                } else {
                    System.out.println("工艺文档有附件");
                    // 获取工艺文档图纸附件
                    ApplicationData appData = null;
                    QueryResult secondResult = ContentHelper.service.getContentsByRole(doc, ContentRoleType.SECONDARY);

                    System.out.println("===ProcessDocument secondResult:" + secondResult.size());
                    String docName = "";
                    while (secondResult.hasMoreElements()) {
                        ContentItem attachment = (ContentItem) secondResult.nextElement();
                        if (attachment instanceof ApplicationData) {
                            appData = (ApplicationData) attachment;
                            if (appData != null) {
                                docName = appData.getFileName();
                                System.out.println("===appData:" + docName);
                                if (docName != null && !"".equals(docName)) {
                                    docName = subFileName(docName);
                                    System.out.println("docName====>>>" + docName);
                                }
                            }
                            break;
                        }
                    }

                    System.out.println("========appData=========" + appData);
                    //获取说明文档
                    QueryResult queryResult = PersistenceHelper.manager.navigate(doc, "describedBy", WTDocumentDependencyLink.class, false);
                    if (queryResult != null) {
                        System.out.println("获取说明文档 queryResult:" + queryResult);
                        System.out.println("获取说明文档 queryResult size:" + queryResult.size());
                        System.out.println("获取说明文档 queryResult hasMoreElements:" + queryResult.hasMoreElements());

                        while (queryResult.hasMoreElements()) {
                            WTDocumentDependencyLink link = (WTDocumentDependencyLink) queryResult.nextElement();

                            WTDocument relateDoc = link.getDependsOn();
                            String relateSoftType = GenericUtil.getTypeId(relateDoc);
                            if (relateSoftType.contains(originDrawingDoc) && relateDoc.isLatestIteration()) {
                                relateDoc = GenericUtil.getWTDocument(relateDoc.getNumber());
                                if (appData != null && relateDoc != null) {
                                    System.out.println("========relateDoc=========" + relateDoc.getNumber() + "," + relateDoc.getName());
                                    //更新工艺类原档图纸的主内容
                                    updateContent(relateDoc, appData);
                                }
                            }
                        }

                        //旧工艺文档不存在工艺类原档图纸时，新建link
                    }
                    if (queryResult != null && queryResult.size() == 0) {

                        System.out.println("旧工艺文档不存在工艺类原档图纸时，新建link");
                        // 创建原档图纸
                        System.out.println("修改工艺文件-doc位置：" + doc.getFolderPath());
                        System.out.println("修改工艺文件-docFolderPath：" + docFolderPath);

                        WTDocument document = createOriginDrawingDoc(doc, docName, originDrawingDoc, docFolderPath);
                        if (document != null && appData != null) {
                            // 原档图纸挂载主内容
                            document = addContent(document, appData);
                            // 创建说明关系
                            createDescripLink(document, doc);
                        }
                    }
                }

            }

            System.out.println("即将工艺文档移除附件：");
            // 工艺文档移除附件
            if (doc != null) {
                System.out.println("工艺文档移除附件：" + doc.getName());
                deleteContent(doc);
            }
        }
        System.out.println("===修改工艺文件 ending===maggie");

    }

    /**
     * 更新主内容 及原档图纸名称
     *
     * @param doc
     * @param data
     * @return
     */

    public static WTDocument updateContent(WTDocument doc, ApplicationData data) {
        if (doc != null) {
            System.out.println("========updateContent=========doc:" + doc.getName());
            try {
                System.out.println("删除主内容：");
                //删除主内容
                ContentHolder contentholder = doc;
                contentholder = ContentHelper.service.getContents(contentholder);
                List contentForTargetList = ContentHelper.getContentListAll(contentholder);
                System.out.println("contentForTargetList:" + contentForTargetList);
                for (int i = 0; i < contentForTargetList.size(); i++) {
                    ContentItem contentItem = (ContentItem) contentForTargetList.get(i);
                    System.out.println("contentItem role:" + contentItem.getRole());
                    if (("PRIMARY").equals(contentItem.getRole().toString())) {
                        ContentServerHelper.service.deleteContent(contentholder, contentItem);
                        break;
                    }
                }
                System.out.println("更新主内容：");
                //新增主内容
                ApplicationData newData = ApplicationData.newApplicationData(doc);
                newData.setRole(ContentRoleType.PRIMARY);
                if (data != null) {
                    System.out.println("==========data:" + data.getFileName());
                    String path = FileDownloadUtil.FolderPath + File.separator + PRIMARY_FILE_TEMP + File.separator
                            + PROCESS_DOCUMENTS + File.separator + data.getFileName();

                    System.out.println("更新主内容文件路径：" + path);

                    boolean flag = downloadPrimaryFile(doc, PROCESS_DOCUMENTS, data.getFileName(), data);// 下载主内容到指定文件夹

                    System.out.println("下载住内容到指定文件夹-flag：" + flag);
                    if (flag) {
                        newData.setFileName(data.getFileName());
                        newData.setUploadedFromPath(path);
                        newData = ContentServerHelper.service.updateContent(doc, newData, path);
                    }

                    PersistenceHelper.manager.save(newData);

                    doc = (WTDocument) PersistenceHelper.manager.refresh((Persistable) doc);

                    System.out.println("新增主内容 doc Name" + doc.getName());
                    PersistenceServerHelper.manager.update(doc);
                    System.out.println("===========addContent end:" + doc);
                    File file = new File(path);
                    if (file.exists()) {
                        file.delete();
                    }
                    //重置 原档图纸名称
                    GenericUtil.setWTDocName(doc, subFileName(data.getFileName()));

                    System.out.println("重置之后的名称：" + doc.getName());
                }
            } catch (Exception e) {
                LOGGER.error("", e);
                e.printStackTrace();
            }
        }
        return doc;
    }

    /**
     * 截取文件名，去除后缀
     *
     * @param fileName
     * @return
     */
    private static String subFileName(String fileName) {
        int dot = fileName.lastIndexOf('.');
        if ((dot > -1) && (dot < (fileName.length()))) {
            return fileName.substring(0, dot);
        }
        return fileName;
    }


    /**
     * 对样品承认书编辑之后名称的修改
     * @param targetObj
     * @param eventType
     */
    public static void changeAcknowledgmentName(Object targetObj,String eventType) {
        if (eventType.equals(PersistenceManagerEvent.POST_MODIFY)) {
            WTDocument doc;
            if (targetObj instanceof WTDocument) {
                System.out.println("更改样品承认书的名称开始llll");
                doc = (WTDocument) targetObj;
                QueryResult primary;
                try {
                    String softType = GenericUtil.getTypeId(doc);
                    // 判断类型为工艺类文件
                    System.out.println("判断doc是什么类型:" + softType);
                    if (!softType.contains("cn.tianma.Acknowledgment")) {
                        return;
                    }
                    primary = ContentHelper.service.getContentsByRole(doc, ContentRoleType.PRIMARY);
                    ContentItem attachmentPrimary = (ContentItem) primary.nextElement();
                    //对文档名称更改
                    if (attachmentPrimary instanceof ApplicationData) {
                        ApplicationData appDataPrimary = (ApplicationData) attachmentPrimary;
                        String appPrimaryName = appDataPrimary.getFileName();
                        appPrimaryName = subFileName(appPrimaryName);
                        System.out.println("样品承认书-appDataPrimary Name:" + appPrimaryName);
                        GenericUtil.setWTDocName(doc, appPrimaryName);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }
    }
}
